<?php
namespace frontend\models;

use bc\ar\DActiveRecord;
use bc\util\AppConst;
class UserOrder extends DActiveRecord
{
	/**
	 * @inheritdoc
	 */
	public static function tableName()
	{
		return 'bc_user_order';
	}
	
	/**
	 * 根据订单编号查询
	 * @author pwstrick
	 */
	public function findByOrderId($order_id)
	{
		return static::findOne(['order_id' => $order_id]);
	}
	
	/**
	 * 更新状态字段 根据订单流水号 不是订单号
	 * @author pwstrick
	 */
	public function pay($order_number, $pay_order_no) {
		
		$row = array(
			'pay_status' => AppConst::PAY_STATUS_PAY,
			'pay_order_no' => $pay_order_no
		);
		$where_str = 'order_number=:order_number AND pay_status=:pay_status';
		$where = array(
			':order_number' => $order_number,
			':pay_status' => AppConst::PAY_STATUS_UNPAY
		);
		return static::updateAll($row, $where_str, $where) > 0;
	}
	
	/**
	 * 判断未支付订单是否已经存在了
	 * @author pwstrick
	 */
	public function exist($uid, $pay_type, $product_id, $pay_status) {
		$where = array(
			'uid' => $uid,
			'pay_type' => $pay_type,
			'product_id' => $product_id,
			'pay_status' => $pay_status,
			'status' => 1
		);
		return static::findOne($where);
	}
	
	/**
	 * 获取到今日订单最大的数量
	 * @author pwstrick
	 */
	public function dayno() {
		$start = date('Y-m-d', time()) . ' 00:00';
		$end = strtotime('+1 day', time());
		$end = date('Y-m-d', $end) . ' 00:00';
		
		$start = strtotime($start);
		$end = strtotime($end);
		$where = sprintf('create_time >= %d AND create_time < %d', $start, $end);
		return static::find($where)->max('day_no');
	}
	
}